<?php

namespace app\test628\controller\admin;

use app\test512\controller\TPOSS;
use app\test628\model\Comments;
use think\Controller;
use think\Request;

class Issue extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $arc_id = 1;
        if (empty($arc_id)) {
            return array();
        }
        $res = Comments::where(array('arc_id' => $arc_id))->order('add_time ASC')->select()->toArray();
        $dataList = $stack = array();
        if ($res) {
            foreach ($res as $k => $v) {   //先将评论的数据进行入库（即comment_id=0）
                if ($v['comment_id'] == 0) {
                    $v['_level'] = 0;   //设置层级数
                    $v['_root'] = $v['id'];   //标识评论id
                    array_push($stack, $v);   //入栈
                    unset($res[$k]);
                }
            }

            while (!empty($stack)) {

                $node = array_pop($stack);   //出栈

                $dataList[] = $node;

                foreach ($res as $_k => $_v) {
                    if ($_v['comment_id'] == $node['id']) {
                        $_v['_level'] = $node['_level'] + 1;   //设置层级数
                        $_v['_root'] = $node['_root'];   //标识评论id
                        array_push($stack, $_v);   //入栈
                        unset($res[$_k]);

                    }
                }
            }
        }


        return view('issue/index',compact('dataList'));
    }


    public function getCommlist()
    {



    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('images');

        // 移动到框架应用根目录/public/uploads/ 目录下

        $info = $file->move('uploads');
        if ($info) {


            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            $img = $info->getFilename();
        }

        $obj = new TPOSS();

        $rest = $obj->uploadOssFile('yw666', $img);

        $data['images'] = $rest['info']['url'] . '?x-oss-process=style/400-300';

        $res = \app\test628\model\Issue::create($data);

        if ($res) {
            return json(['code' => 200, 'msg' => '添加成功!', 'data' => '']);
        } else {
            return json(['code' => 500, 'msg' => '添加失败!', 'data' => '']);

        }
    }


    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
